package cn.yitun.chat.yitunserverimcore.general.main.push.file;

import cn.yitun.chat.yituncommonmessage.impl.HeadImpl;
import cn.yitun.chat.yituncommonmessage.push.PushMessage;
import cn.yitun.chat.yitunservercommonlib.constant.ActionTypeConstant;
import cn.yitun.chat.yitunservercommonlib.dataentity.dto.common.FileDataDto;
import cn.yitun.chat.yitunservercommonlib.enums.control.push.file.FilePushMethodEnum;
import cn.yitun.chat.yitunserverimcore.bridge.net.WriteHandler;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class FilePush {

	@Resource
	WriteHandler writeHandler;
	//1.5.005
	final String action = ActionTypeConstant.FILE_ACTION_PUSH;

	/**
	 * 获取对方接收文件信息
	 * @param key
	 * @param query
	 */
	public void pushGetOppositeFileAcceptInfo(Long key, FileDataDto query) {
        Long receiveUserId = query.getReceiveUserId();
        Long sendUserId = query.getSendUserId();
		HeadImpl head = new HeadImpl();
		head.setAction(action);
		head.setMethod(FilePushMethodEnum.GET_OPPOSITE_FILE_ACCEPT.getMethod());
		head.setTimestamp(System.currentTimeMillis());
		head.setKey(key);

		PushMessage message = new PushMessage();
		message.setHead(head);
		message.bodyPut("receiveUserId", receiveUserId);
		message.bodyPut("sendUserId", sendUserId);
		writeHandler.push(receiveUserId, message);
	}

	/**
	 * 返回文件接收信息
	 * @param key
	 * @param query
	 */
	public void pushReturnFileAcceptInfo(Long key, FileDataDto query) {
		HeadImpl head = new HeadImpl();
		head.setAction(action);
		head.setMethod(FilePushMethodEnum.RETURN_FILE_ACCEPT.getMethod());
		head.setTimestamp(System.currentTimeMillis());
		head.setKey(key);

		PushMessage m = new PushMessage();
		m.setHead(head);
		m.put("receiveUserId", query.getReceiveUserId());
		m.put("sendUserId", query.getSendUserId());
		m.put("acceptTypeList", query.getAcceptTypeList());
		writeHandler.push(query.getReceiveUserId(), m);
	}

	/**
	 * 发送离线文件
	 * @param key
	 * @param query
	 */
	public void pushSendOfflineFile(Long key, FileDataDto query) {
		HeadImpl head = new HeadImpl();
		head.setAction(action);
		head.setMethod(FilePushMethodEnum.SEND_OFFLINE_FILE.getMethod());
		head.setTimestamp(System.currentTimeMillis());
		head.setKey(key);

		PushMessage message = new PushMessage();
		message.setHead(head);
		message.bodyPut("fileData", query);
		writeHandler.push(query.getReceiveUserId(), message);
	}

	/**
	 * 响应
	 * @param key
	 * @param query
	 */
	public void pushResponse(Long key, FileDataDto query) {
		HeadImpl head = new HeadImpl();
		head.setAction(action);
		head.setMethod(FilePushMethodEnum.RESPONSE.getMethod());
		head.setTimestamp(System.currentTimeMillis());
		head.setKey(key);

		PushMessage m = new PushMessage();
		m.setHead(head);
		m.put("receiveUserId", query.getReceiveUserId());
		m.put("sendUserId", query.getSendUserId());
		m.put("actionType", query.getActionType());
		m.put("code", query.getCode());
		m.put("message", query.getMessage());
		writeHandler.push(query.getReceiveUserId(), m);
	}

	/**
	 * 拒绝
	 * @param key
	 * @param query
	 */
	public void pushCancel(Long key, FileDataDto query) {
		HeadImpl head = new HeadImpl();
		head.setAction(action);
		head.setMethod(FilePushMethodEnum.CANCEL.getMethod());
		head.setTimestamp(System.currentTimeMillis());
		head.setKey(key);

		PushMessage message = new PushMessage();
		message.setHead(head);
        message.put("receiveUserId", query.getReceiveUserId());
        message.put("sendUserId", query.getSendUserId());
		writeHandler.push(query.getReceiveUserId(), message);
	}

}
